import java.io.File;
import java.util.*;


public class Words {
	
	private static Map<Map<Character, Integer>, Set<String>> connections = new HashMap<Map<Character, Integer>, Set<String>>();

	public static Graph loadGraph(String path) {
		Graph g = new Graph();
		In in = new In(new File(path + ".dat"));
		while (!in.isEmpty()) {
			g.add(in.readLine());
		}
		in.close();
		Set<String> nodes = g.getNodes();
		for (String n : nodes) {
			Map<Character, Integer> map = getMap(n);
			g.addEdges(n, getConnections(map, g));
		}
		return g;
	}
	
	private static Set<String> getConnections(Map<Character, Integer> map, Graph g) {
		if (connections.containsKey(map)) return connections.get(map);
		Set<String> set = new HashSet<String>();
		for (String n : g.getNodes()) {
			if (connects(map, n)) set.add(n);
		}
		return set;
	}
	
	private static Map<Character, Integer> getMap(String str) {
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (int i = 1; i<5; i++) {
			char c = str.charAt(i);
			Integer integ = map.get(c);
			if (integ == null) integ = 0;
			map.put(c, integ + 1);
		}
		return map;
	}
	
	private static boolean connects(Map<Character, Integer> map, String test) {
		for (char c : map.keySet()) {
			int lastOccurrence = -1;
			int times = map.get(c);
			for (int i = 0; i<times; i++) {
				int occurrence = test.indexOf(c, lastOccurrence + 1);
				if (occurrence == -1) return false;
				lastOccurrence = occurrence;
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
		Stopwatch sw = new Stopwatch();
		Graph g = loadGraph(args[0]);
		//System.out.println(g);
		In in = new In(new File(args[0] + "-test.in"));
		while (!in.isEmpty()) {
			String from = in.readString();
			String to = in.readString();
			System.out.println(g.BFS(from, to));
		}
		System.out.println(sw.elapsedTime());
		in.close();
	}


static class Graph {
	private Map<String, Set<String>> nodes = new HashMap<String, Set<String>>();
	
	public void add(String str) {
		nodes.put(str, null);/////
	}
	
	public Set<String> getNodes() {
		return nodes.keySet();
	}
	
	public void addEdges(String n, Set<String> set) {
		Set<String> origSet = nodes.get(n);
		if (origSet == null) nodes.put(n, set);
		else origSet.addAll(set);
	}
	
	public int BFS(String from, String to) {
		if (from.equals(to)) return 0;
		ArrayDeque<String> que = new ArrayDeque<String>();
		Map<String, Integer> queued = new HashMap<String, Integer>();
		que.offer(from);
		queued.put(from, 0);
		while (!que.isEmpty()) {
			String test = que.poll();
			int tier = queued.get(test) + 1;
			for (String n : nodes.get(test)) {
				if (n.equals(to)) return tier;
				if (!queued.containsKey(n)) {
					queued.put(n, tier);
					que.offer(n);
				}
			}
		}
		return -1;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Graph of nodes:\n");
		for (String n : nodes.keySet()) {
			sb.append(n).append(" has edges to ").append(nodes.get(n)).append("\n");
		}
		return sb.toString();
	}
}
}